package com.fr.lintcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.junit.Test;


/**
 * @author 付荣
 * @version 创建时间：2019年1月31日 下午2:47:58
 */
public class Q864 {
	/**
     * @param array: the input array
     * @return: the sorted array
     */
    public int[][] multiSort(int[][] array) {
    	int length = array.length;
        Map<Integer, Integer> map =  new HashMap<>();
        for(int [] a : array){
        	map.put(a[0], a[1]);
        }
        List<Map.Entry<Integer, Integer>> list = new ArrayList<>();
        list.addAll(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>(){

			@Override
			public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
				if (o1.getValue() == o2.getValue()){
					return o1.getKey() - o2.getKey();
				}
				return o2.getValue() - o1.getValue();
			}
        	
        });
        int [][]rs = new int[length][2];
        int i =0;
        for(Map.Entry<Integer, Integer> entry : list){
        	rs[i][0] = entry.getKey();
        	rs[i][1] = entry.getValue();
        	i++;
        }
        return rs;
    }
    
    @Test
    public void testA(){
    	int [][] array = {{7,66},{4,77},{3,63},{5,81},{1,88},{9,86},{6,88},{2,82},{8,55},{10,95}};
    	int [][] rs = multiSort(array);
    	for(int [] a : rs){
    		System.out.println(a[0] +","+a[1]);
    	}
    	
    }
}
